<template>
	<div class="game-container report-container">
		<div class="view-container dealer-container">
			<div class="content">
				保险赔率-德州扑克 / AOF德州
				<el-divider></el-divider>
				<InsureConfigItem
					:gameType="2001"
					:configLoading="texasPokerLoading"
					:configListForm="texasConfigForm"
					:configList="texasConfigList"
					@getConfigForm="getTexasPokerInsureOddsForm"
					@submit="submitTexasPokerInsureOdds"
				></InsureConfigItem>
			</div>

			<div class="content">
				保险赔率-短牌 / AOF短牌
				<el-divider></el-divider>
				<InsureConfigItem
					:gameType="2002"
					:configLoading="shortCardLoading"
					:configListForm="shortConfigForm"
					:configList="shortConfigList"
					@getConfigForm="getShortCardInsureOddsForm"
					@submit="submitShortCardInsureOdds"
				></InsureConfigItem>
			</div>
		</div>
	</div>
</template>

<script>
import InsureConfigItem from './components/InsureConfigItem'

export default {
	components: {
		InsureConfigItem
	},
	data() {
		return {
			configDetail: {},
			texasConfigForm: {},
			shortConfigForm: {},
			texasConfigList: [],
			shortConfigList: [],
			texasPokerLoading: false,
			shortCardLoading: false
		}
	},
	computed: {},
	created() {
		this.getInsuranceOddsList()
	},
	methods: {
		// 德州扑克赔率保存
		submitTexasPokerInsureOdds(params, callback) {
			this.texasPokerLoading = true
			this.$api
				.updateInsuranceOddsBend(params)
				.then((res) => {
					if (res?.code === 200) {
						this.$message.success('保存成功')
						callback(true)
						this.getTexasPokerInsureOddsForm()
					} else {
						this.$message.error('保存失败')
						callback(false)
					}
				})
				.catch(() => {
					callback(false)
				})
				.finally(() => {
					this.texasPokerLoading = false
				})
		},
		// 短牌赔率保存
		submitShortCardInsureOdds(params, callback) {
			this.shortCardLoading = true
			this.$api
				.updateInsuranceOddsBend(params)
				.then((res) => {
					if (res?.code === 200) {
						this.$message.success('保存成功')
						callback(true)
						this.getShortCardInsureOddsForm()
					} else {
						this.$message.error('保存失败')
						callback(false)
					}
				})
				.catch(() => {
					callback(false)
				})
				.finally(() => {
					this.shortCardLoading = false
				})
		},
		// 获取德州扑克赔率
		getTexasPokerInsureOddsForm() {
			this.texasPokerLoading = true
			this.$api
				.getInsuranceOddsListBend({ gameType: 2001 })
				.then((res) => {
					if (res?.code === 200) {
						const formatTexas = this.formatGameTypeOdds(
							res?.data?.insureOddsInfoMap['2001']
						)
						this.texasConfigForm = formatTexas.configForm
					}
				})
				.catch(() => {})
				.finally(() => {
					this.texasPokerLoading = false
				})
		},
		// 获取短牌赔率
		getShortCardInsureOddsForm() {
			this.shortCardLoading = true
			this.$api
				.getInsuranceOddsListBend({ gameType: 2002 })
				.then((res) => {
					if (res?.code === 200) {
						const formatShort = this.formatGameTypeOdds(
							res?.data?.insureOddsInfoMap['2002']
						)
						this.shortConfigForm = formatShort.configForm
					}
				})
				.catch(() => {})
				.finally(() => {
					this.shortCardLoading = false
				})
		},
		// 首次加载获取德州扑克和短牌赔率数据
		getInsuranceOddsList() {
			this.texasPokerLoading = true
			this.texasConfigForm = {}
			this.texasConfigList = []

			this.shortCardLoading = true
			this.shortConfigForm = {}
			this.shortConfigList = []
			this.$api
				.getInsuranceOddsListBend({})
				.then((res) => {
					if (res?.code === 200) {
						// 德州扑克配置
						const formatTexas = this.formatGameTypeOdds(
							res?.data?.insureOddsInfoMap['2001']
						)
						this.texasConfigForm = formatTexas.configForm
						this.texasConfigList = formatTexas.configList

						// 短牌配置
						const formatShort = this.formatGameTypeOdds(
							res?.data?.insureOddsInfoMap['2002']
						)
						this.shortConfigForm = formatShort.configForm
						this.shortConfigList = formatShort.configList
					}
				})
				.catch(() => {})
				.finally(() => {
					this.texasPokerLoading = false
					this.shortCardLoading = false
				})
		},
		// 处理数据
		formatGameTypeOdds(gameTypeList) {
			const configForm = {}
			const configList = []
			// 处理后端传递过的数据
			if (Array.isArray(gameTypeList)) {
				gameTypeList.forEach((item, index) => {
					// 自定义key
					const formKey = [
						'odds',
						item.id,
						item.oddsType,
						item.outs,
						item.gameType
					].join('-')
					configForm[formKey] = item.odds
					// odds数据
					const newItem = {
						key: formKey,
						id: item.id,
						outs: item.outs,
						odds: item.odds,
						gameType: item.gameType
					}
					const configRow = configList.find((row) => {
						return item.oddsType === row.oddsType
					})
					// 如果已添加则加入新的配置
					if (configRow) {
						configRow.oddsData.push(newItem)
					} else {
						const configItem = {
							oddsType: item.oddsType,
							oddsData: [newItem]
						}
						configList.push(configItem)
					}
				})
			}
			// 对configList种的oddsData进行排序
			configList.forEach((item) => {
				item.oddsData.sort((a, b) => {
					return a.outs - b.outs
				})
			})

			return { configForm, configList }
		}
	}
}
</script>
